package com.jinlejia.erp.ui.new_house.apply;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.common.WrapContentLinearLayoutManager;
import com.jinlejia.erp.databinding.ActivityApplyDetailBinding;
import com.jinlejia.erp.ui.new_house.adapter.ApplyTimeLineAdapter;
import com.jinlejia.erp.utils.DataFilter;
import com.jinlejia.erp.utils.LeftLineDecoration;
import com.jinlejia.erp.utils.RecommendState;
import com.zhy.http.okhttp.OkHttpUtils;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

import okhttp3.Call;

/**
 * 申请详情
 */
public class ApplyDetailActivity extends AppCompatActivity implements View.OnClickListener {

    private ApplyTimeLineAdapter adapter;
    private String dynamicCode;

    private ActivityApplyDetailBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_apply_detail);
        binding.idBack.setOnClickListener(this::onViewClicked);
        binding.idShare.setOnClickListener(this::onViewClicked);
        EventBus.getDefault().register(this);
        dynamicCode = getIntent().getStringExtra("dynamicCode");
        initViews();
        initData();
    }


    protected void initViews() {

        binding.idApplyTimeline.setHasFixedSize(true);
        binding.idApplyTimeline.setLayoutManager(new WrapContentLinearLayoutManager(this));
        binding.idApplyTimeline.addItemDecoration(new LeftLineDecoration(this));
        ArrayList<Map<String, String>> data = new ArrayList<>();
        adapter = new ApplyTimeLineAdapter(data);
        binding.idApplyTimeline.setAdapter(adapter);
    }

    protected void initData() {

        OkHttpUtils.get().url(ApiConfig.APPLY_DETAIL)
                .addParams("dynamicCode", dynamicCode)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        call.cancel();
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(ApplyDetailActivity.this, response)) {
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            //客户信息
                            JSONObject cInfo = data.getJSONObject("customerDynamic");
                            binding.idCName.setText(cInfo.getString("customerName"));
                            binding.idCPhone.setText(cInfo.get("customerTel").toString());
                            binding.idProjectName.setText(cInfo.getString("houseName"));
                            binding.idApplyState.setText(RecommendState.getStateStr(String.valueOf(cInfo.get("state"))));

                            ArrayList<Map<String, String>> dataList = JSON.parseObject(data.getJSONArray("list").toJSONString(), new TypeReference<ArrayList<Map<String, String>>>() {
                            });
                            Collections.sort(dataList, (lhs, rhs) -> (rhs.get("createTime").compareTo(lhs.get("createTime"))));
                            adapter.setData(dataList);
                        }
                    }
                });

    }

//    @Subscriber(tag = "TAG_APPLY_DETAIL")
//    public void detail(String response) {
//         LogUtil.d("TAG_APPLY_DETAIL === > " + response);
//        ArrayList<Map<String, String>> dataList = new ArrayList<>();
//        try {
//            JSONObject jsonObject = new JSONObject(response);
//            if (jsonObject.getString("code").equals("200")) {
//                JSONObject data = jsonObject.getJSONObject("data");
//                //客户信息
//                JSONObject cInfo = data.getJSONObject("customerDynamic");
//                idCName.setText(cInfo.getString("customerName"));
//                idCPhone.setText(cInfo.get("customerTel").toString());
//                idProjectName.setText(cInfo.getString("houseName"));
//                idApplyState.setText(RecommendState.getStateStr(cInfo.get("idApplyState").toString()));
//
//                //动态列表
//                JSONArray dList = data.getJSONArray("list");
//                for (int i = 0, j = dList.length(); i < j; i++) {
//                    JSONObject object = (JSONObject) dList.get(i);
//                    Map<String, String> map = new HashMap<>();
//                    map.put("time", object.get("createTime").toString());
//                    map.put("idApplyState", RecommendState.getStateStr(object.getString("resourceState").toString()));
//                    map.put("resourceState", object.get("resourceState").toString());
//                    map.put("remark", object.get("remark").toString());
//                    dataList.add(map);
//                }
//                Collections.sort(dataList, new Comparator<Map<String, String>>() {
//                    @Override
//                    public int compare(Map<String, String> lhs, Map<String, String> rhs) {
//                        return (rhs.get("time").toString().compareTo(lhs.get("time").toString()));
//                    }
//                });
//                adapter.setData(dataList);
//            }
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_back:
                //返回
                finish();
                break;
            case R.id.id_share:
                //分享
                break;
            case R.id.id_re_apply:
                //重新申请
                break;
            default:
                break;
        }
    }


    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.id_back:
                finish();
                break;
            case R.id.id_share:
                break;
        }
    }
}
